//-------------------------------------------------------------------------
// File: Commandline.cpp
//
// Descr.: - todo -
//
// Author: Stefan Leihkauf
// Date:   23.03.2014
//-------------------------------------------------------------------------
#include "cb_pch.h"
#include "Commandline.h"
#include "common/util/Util.h"

//-------------------------------------------------------------------------
// options and switches are introduced by this
boost::array<StdString, 2> OptionDelimiters = {{ _T("--"), _T("-") }};

//-------------------------------------------------------------------------
// delimiters used to seperate an option and its value
const StdString OptionKeyValueDelimiters =  _T("=");

//-------------------------------------------------------------------------
// multiple option values are delimited by this
const StdString ValueValueDelimiter = _T("|");

namespace cb {

using namespace cb::common::util;

//-------------------------------------------------------------------------
Commandline::Commandline()
{
}

//-------------------------------------------------------------------------
Commandline::Commandline(int numArgs, const TCHAR** ppArgv)
{
}

//-------------------------------------------------------------------------
Commandline::~Commandline()
{
}

//-------------------------------------------------------------------------
void Commandline::Parse(const StringList& cmdLine)
{
    if (cmdLine.empty())
        return; // empty args

    for (unsigned i = 0; i < cmdLine.size(); i++) {
        StdString strTemp(cmdLine[i]);

        StringTrimLeftRight(strTemp);

        if (IsOption(strTemp)) {
            // parse option
        } else if (IsSwitch(strTemp)) {
            this->AddSwitch(strTemp);
        } else {
            _arguments.push_back(strTemp);
        }
    }
}

//-------------------------------------------------------------------------
bool Commandline::IsSwitch(const StdString& str) const
{
    if (str.empty())
        return false;

    for (unsigned i = 0; i < OptionDelimiters.size(); i++) {
        if (str.find(OptionDelimiters[i]) == 0) {
            return true;
        }
    }

    return false;
}

//-------------------------------------------------------------------------
bool Commandline::IsOption(const StdString& str) const
{
    if (IsSwitch(str)) {
        StringList sl = StringSplit(str, OptionKeyValueDelimiters);
        return (false == sl.empty());
    }

    return false;
}

//-------------------------------------------------------------------------
void Commandline::AddSwitch(const StdString& str)
{
    if (str.empty())
        return;

    StdString strTemp(RemoveOptionDelimiter(str));

    // add switch
    if (false == strTemp.empty())
        _options.push_back(Option(strTemp));
}

//-------------------------------------------------------------------------
void Commandline::AddOption(const StdString& str)
{
    if (str.empty())
        return;

    StdString strTemp(RemoveOptionDelimiter(str));

}

//-------------------------------------------------------------------------
StdString Commandline::RemoveOptionDelimiter(const StdString& str) const
{
    if (str.empty())
        return StdString();

    StdString strTemp(str);

    for (unsigned i = 0; i < OptionDelimiters.size(); i++) {
        if (strTemp.find(OptionDelimiters[i]) == 0) {
            strTemp = strTemp.substr(0, OptionDelimiters[i].length());
        }
    }

    return strTemp;
}

} // namespace
